package com.saiaweather.android.util;

import android.content.Context;
import android.text.TextUtils;

import com.saiaweather.android.db.City;
import com.saiaweather.android.db.County;
import com.saiaweather.android.db.Province;
import com.saiaweather.android.db.SaiaWeatherDB;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Utilty {

    /*
    private  int id;
    private  String provinceName;
    private  int provinceCode;
     */

    //处理服务器返回的Province数据,有则保存在本地数据库，无则返回false
    public static boolean handleProvinceResponse(String response, Context context) {
        if (!TextUtils.isEmpty(response)) {
            JSONArray allProvinces = null;
            try {
                allProvinces = new JSONArray(response);
                for (int i = 0; i < allProvinces.length(); i++) {
                    JSONObject provinceObject = allProvinces.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getString("id"));
                    SaiaWeatherDB saiaWeatherDB = SaiaWeatherDB.getInstance(context);
                    saiaWeatherDB.saveProvince(province);

                }
                return true;

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
     private int id;
    private String cityName;
    private int cityCode;
    private int provinceId;
     */
    //处理服务器返回的City数据
    public static boolean handleCityResponse(Context context, String response, int provinceId) {
        if (!TextUtils.isEmpty(response)) {
            JSONArray allcities=null;
            try {
                allcities = new JSONArray(response);
                for (int i = 0; i < allcities.length(); i++) {
                    JSONObject cityObject = allcities.getJSONObject(i);
                    City city = new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getString("id"));
                    city.setProvinceId(provinceId);
                    SaiaWeatherDB saiaWeatherDB = SaiaWeatherDB.getInstance(context);
                    saiaWeatherDB.saveCity(city);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


    /*
    private int id;
    private String countyName;
    private String weatherId;
    private  int cityId;
     */
    //处理服务器返回的county数据
    public static boolean handleCountyResponse(Context context, String response, int cityId) {
        if (!TextUtils.isEmpty(response)) {
            JSONArray allCounties=null;
            try {
                allCounties = new JSONArray(response);
                for (int i = 0; i < allCounties.length(); i++) {
                    JSONObject countyObject = allCounties.getJSONObject(i);
                    County county = new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    SaiaWeatherDB saiaWeatherDB = SaiaWeatherDB.getInstance(context);
                    saiaWeatherDB.saveCounty(county);
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}
